package leetcode.code1422;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int maxScore1(String s) {
		char[] cs = s.toCharArray();
		int len = cs.length;
		int[] dp = new int[len];
		dp[len - 1] = cs[len - 1] == '1' ? 1 : 0;
		for (int i = len - 2; i > 0; i--) {
			dp[i] = cs[i] == '1' ? dp[i + 1] + 1 : dp[i + 1];
		}
		int cnt = 0, ans = 0;
		for (int i = 0; i < len - 1; i++) {
			cnt += cs[i] == '0' ? 1 : 0;
			ans = Math.max(ans, cnt + dp[i + 1]);
		}
		return ans;
	}

	public int maxScore(String s) {
		char[] cs = s.toCharArray();
		int len = cs.length;
		int max = 0, zero = 0;
		for (int i = 0; i < len; i++) {
			zero += cs[i] == '0' ? 1 : 0;
			if (i < len - 1 && (zero << 1) - i > max) {
				max = (zero << 1) - i;
			}
		}
		// len - 1 - i - (zero - z) + z;
		// len - 1 -zero + 2*z - i
		return len - 1 + max - zero;
	}

	@Override
	public void debug4() {
		H.compare(1, this.maxScore("00"));
		H.compare(3, this.maxScore("1111"));
		H.compare(5, this.maxScore("00111"));
		H.compare(5, this.maxScore("011101"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
